package com.zycfc.zsf.boot.mybatis.generator.codegen.util;

import java.io.*;

public class FileHelper
{
    public static String readFile(final String fileName, final String charset) {
        try {
            final File file = new File(fileName);
            final StringBuffer sb = new StringBuffer();
            final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), charset));
            String str;
            while ((str = in.readLine()) != null) {
                sb.append(str + "\r\n");
            }
            in.close();
            return sb.toString();
        }
        catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
    
    public static String getCharset(final File file) {
        String charset = "GBK";
        final byte[] first3Bytes = new byte[3];
        try {
            boolean checked = false;
            final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            bis.mark(0);
            int read = bis.read(first3Bytes, 0, 3);
            if (read == -1) {
                return charset;
            }
            if (first3Bytes[0] == -1 && first3Bytes[1] == -2) {
                charset = "UTF-16LE";
                checked = true;
            }
            else if (first3Bytes[0] == -2 && first3Bytes[1] == -1) {
                charset = "UTF-16BE";
                checked = true;
            }
            else if (first3Bytes[0] == -17 && first3Bytes[1] == -69 && first3Bytes[2] == -65) {
                charset = "UTF-8";
                checked = true;
            }
            bis.reset();
            if (!checked) {
                int loc = 0;
                while ((read = bis.read()) != -1) {
                    ++loc;
                    if (read >= 240) {
                        break;
                    }
                    if (128 <= read && read <= 191) {
                        break;
                    }
                    if (192 <= read && read <= 223) {
                        read = bis.read();
                        if (128 <= read && read <= 191) {
                            continue;
                        }
                        break;
                    }
                    else {
                        if (224 > read || read > 239) {
                            continue;
                        }
                        read = bis.read();
                        if (128 > read || read > 191) {
                            break;
                        }
                        read = bis.read();
                        if (128 <= read && read <= 191) {
                            charset = "UTF-8";
                            break;
                        }
                        break;
                    }
                }
            }
            bis.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return charset;
    }
    
    public static void backupFile(final String filePath) {
        final File file = new File(filePath);
        if (file.exists()) {
            String path = filePath + ".001";
            File tmp = new File(path);
            int i = 1;
            while (tmp.exists()) {
                ++i;
                final int len = 3 - String.valueOf(i).length();
                String ext = String.valueOf(i);
                for (int k = 0; k < len; ++k) {
                    ext = "0" + ext;
                }
                path = filePath + "." + ext;
                tmp = new File(path);
            }
            copyFile(filePath, path);
        }
    }
    
    public static void writeFile(final File file, final String charset, final String content) throws IOException {
        final Writer writer = new OutputStreamWriter(new FileOutputStream(file), charset);
        writer.write(content);
        writer.close();
    }
    
    public static String[] getBySplit(final String content, final String splitTag) {
        final String[] aryRtn = new String[2];
        final int pos = content.lastIndexOf(splitTag);
        aryRtn[0] = content.substring(0, pos);
        aryRtn[1] = content.substring(pos + splitTag.length());
        return aryRtn;
    }
    
    public static boolean copyFile(final String from, final String to) {
        final File fromFile = new File(from);
        final File toFile = new File(to);
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(fromFile);
            fos = new FileOutputStream(toFile);
            final byte[] buf = new byte[4096];
            int bytesRead;
            while ((bytesRead = fis.read(buf)) != -1) {
                fos.write(buf, 0, bytesRead);
            }
            fos.flush();
            fos.close();
            fis.close();
        }
        catch (IOException e) {
            System.out.println(e);
            return false;
        }
        return true;
    }
    
    public static boolean isExistFile(final String dir) {
        boolean isExist = false;
        final File fileDir = new File(dir);
        if (fileDir.isDirectory()) {
            final File[] files = fileDir.listFiles();
            if (files != null && files.length != 0) {
                isExist = true;
            }
        }
        return isExist;
    }
    
    public static File[] getFiles(final String path) {
        final File file = new File(path);
        return file.listFiles();
    }
    
    public static InputStream getInputStream(final String filepath) {
        final File file = new File(filepath);
        final String charset = getCharset(file);
        final String str = readFile(filepath, charset);
        ByteArrayInputStream stream = null;
        try {
            stream = new ByteArrayInputStream(str.getBytes(charset));
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return stream;
    }
}
